package com.itcncp.ucenter.rpc.service.impl;

import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.ucenter.dao.mapper.UserBankInfoMapper;
import com.itcncp.ucenter.dao.model.UserBankInfo;
import com.itcncp.ucenter.dao.model.UserBankInfoExample;
import com.itcncp.ucenter.rpc.api.UserBankInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* UserBankInfoService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class UserBankInfoServiceImpl extends BaseServiceImpl<UserBankInfoMapper, UserBankInfo, UserBankInfoExample> implements UserBankInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserBankInfoServiceImpl.class);

    @Autowired
    UserBankInfoMapper userBankInfoDao;

    /**
     * 绑定银行卡
     * @param userBankInfo
     * @return
     * 0：失败 1成功
     */
    @Override
    public Integer addUserBarkInfo(UserBankInfo userBankInfo){

        //判断银行卡是否已经绑定
        UserBankInfo itemUserBankInfo = userBankInfoDao.selectByPrimaryKey( userBankInfo.getUserId(),userBankInfo.getBankcardNo() );
        //银行卡已存在
        if(null != itemUserBankInfo){

            if(itemUserBankInfo.getBankcardState()!=1){

                return  userBankInfoDao.updateByPrimaryKeySelective( userBankInfo );

            }

            return  0;
        }

        userBankInfo.setUpdateTime( new Date(  ) );

        userBankInfo.setCreateTime( new Date(  ) );

        return  userBankInfoDao.insertSelective( userBankInfo );
    }

}